package com.haiyi.software.app.basic;

import java.util.HashMap;
import java.util.Map;

import org.apache.http.Header;

import android.util.Log;

import com.google.gson.Gson;
import com.haiyi.software.app.MainApplication;
import com.haiyi.software.entity.UserInfo;
import com.haiyi.software.tools.L;
import com.loopj.android.http.AsyncHttpClient;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;

public abstract class ActivityMineBasicSetHead extends
		ActivityMineBasicDelAttch {

	protected abstract void setHeadSuccess(String strReturn);

	private final static String TAG = "FragmentBasicPersonalSetHead";

	private String str_requestURL = null;

	private final String str_requestName = "setHead.php";

	private String Attuid = null;

	protected void setHeadByAttID(String aid) {

		this.Attuid = aid;

		str_requestURL = str_server + str_requestName;

		AsyncHttpClient client = MainApplication.getInstance()
				.getAsyncHttpClient();

		RequestParams params = new RequestParams();

		params.put("args", getJsonArgs());

		client.post(str_requestURL, params, responseHandler);

	}

	private AsyncHttpResponseHandler responseHandler = new AsyncHttpResponseHandler() {

		@Override
		public void onSuccess(int statusCode, Header[] headers,
				byte[] responseBody) {
			// TODO 自动生成的方法存根
			try {
				String str_Return = new String(responseBody, "UTF-8");
				setHeadSuccess(str_Return);

			} catch (Exception e) {
				// TODO 自动生成的 catch 块
				e.printStackTrace();
			}

		}

		@Override
		public void onFailure(int statusCode, Header[] headers,
				byte[] responseBody, Throwable error) {
			// TODO 自动生成的方法存根

		}
	};

	private String getJsonArgs() {
		UserInfo myInfo = MainApplication.getInstance().getMineUserInfo();

		if (myInfo != null) {

			Map<String, Object> validator = new HashMap<String, Object>();

			validator.put("uid", myInfo.getUid());
			validator.put("uname", myInfo.getUname());
			validator.put("pwd", myInfo.getPwd());
			validator.put("uguid", myInfo.getUguid());

			Map<String, Object> argsBody = new HashMap<String, Object>();
			argsBody.put("attuid", this.Attuid);

			Map<String, Object> args = new HashMap<String, Object>();
			args.put("validator", validator);
			args.put("body", argsBody);

			Gson gson = new Gson();

			String s = gson.toJson(args);

			L.i("argsJson", s);

			return s;
		} else {
			return null;
		}
	}
}
